package com.ruoyi.project.modules.parameter.controller;

import java.util.List;

import cn.hutool.core.collection.CollectionUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.project.modules.parameter.domain.ParamsFieldRelation;
import com.ruoyi.project.modules.parameter.service.IParamsFieldRelationService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 数据集下面字段关系Controller
 * 
 * @author smallrain
 * @date 2021-10-30
 */
@Controller
@RequestMapping("/paramsFieldRelation/parameter")
public class ParamsFieldRelationController extends BaseController
{
    private String prefix = "parameter/parameter";

    @Autowired
    private IParamsFieldRelationService paramsFieldRelationService;

    @RequiresPermissions("parameter:parameter:view")
    @GetMapping()
    public String parameter()
    {
        return prefix + "/parameter";
    }

    /**
     * 查询数据集下面字段关系列表
     */
    @RequiresPermissions("parameter:parameter:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ParamsFieldRelation paramsFieldRelation)
    {
        startPage();
        List<ParamsFieldRelation> list = paramsFieldRelationService.selectParamsFieldRelationList(paramsFieldRelation);
        return getDataTable(list);
    }

    /**
     * 导出数据集下面字段关系列表
     */
    @RequiresPermissions("parameter:parameter:export")
    @Log(title = "数据集下面字段关系", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ParamsFieldRelation paramsFieldRelation)
    {
        List<ParamsFieldRelation> list = paramsFieldRelationService.selectParamsFieldRelationList(paramsFieldRelation);
        ExcelUtil<ParamsFieldRelation> util = new ExcelUtil<ParamsFieldRelation>(ParamsFieldRelation.class);
        return util.exportExcel(list, "数据集下面字段关系数据");
    }

    /**
     * 新增数据集下面字段关系
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存数据集下面字段关系
     */
    @RequiresPermissions("parameter:parameter:add")
    @Log(title = "数据集下面字段关系", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ParamsFieldRelation paramsFieldRelation)
    {
        return toAjax(paramsFieldRelationService.insertParamsFieldRelation(paramsFieldRelation));
    }

    /**
     * 修改数据集下面字段关系
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        ParamsFieldRelation paramsFieldRelation = paramsFieldRelationService.selectParamsFieldRelationById(id);
        mmap.put("paramsFieldRelation", paramsFieldRelation);
        return prefix + "/edit";
    }

    /**
     * 修改保存数据集下面字段关系
     */
    @RequiresPermissions("parameter:parameter:edit")
    @Log(title = "数据集下面字段关系", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ParamsFieldRelation paramsFieldRelation)
    {
        return toAjax(paramsFieldRelationService.updateParamsFieldRelation(paramsFieldRelation));
    }

    /**
     * 删除数据集下面字段关系
     */
    @GetMapping( "/remove/{id}")
    @ResponseBody
    public AjaxResult remove(@PathVariable Long id)
    {
        ParamsFieldRelation paramsFieldRelation = new ParamsFieldRelation();
        paramsFieldRelation.setDataRelationId(id);
        List<ParamsFieldRelation> paramsFieldRelationList = this.paramsFieldRelationService.selectParamsFieldRelationList(paramsFieldRelation);
        if (CollectionUtil.isNotEmpty(paramsFieldRelationList)) {
            return AjaxResult.warn("该字段和bff接口或者虚拟用况有关联，请解除关联");
        }
        return toAjax(paramsFieldRelationService.deleteParamsFieldRelationById(id));
    }
}
